import { request } from './client'
import type { AlgorithmProblem, AlgorithmSolution, CreateAlgorithmRequest } from '@/types/api'

// 获取所有算法题
export const getAlgorithms = (params?: {
  page?: number
  limit?: number
  difficulty?: string
  search?: string
}) => {
  return request.get<{
    problems: AlgorithmProblem[]
    total: number
    page: number
    limit: number
  }>('/algorithms', params)
}

// 获取单个算法题详情
export const getAlgorithm = (id: number) => {
  return request.get<AlgorithmProblem>(`/algorithms/${id}`)
}

// 创建算法题
export const createAlgorithm = (data: CreateAlgorithmRequest) => {
  return request.post<AlgorithmProblem>('/algorithms', data)
}

// 更新算法题
export const updateAlgorithm = (id: number, data: Partial<CreateAlgorithmRequest>) => {
  return request.put<AlgorithmProblem>(`/algorithms/${id}`, data)
}

// 删除算法题
export const deleteAlgorithm = (id: number) => {
  return request.delete(`/algorithms/${id}`)
}

// 保存代码解决方案
export const saveCode = (id: number, code: string) => {
  return request.put<AlgorithmSolution>(`/algorithms/${id}/code`, { code })
}

// 获取代码解决方案
export const getCode = (id: number) => {
  return request.get<AlgorithmSolution>(`/algorithms/${id}/code`)
}